﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Alibobo.Util;
using System.Collections;
namespace TaobaoPick
{
    public class TaobaoSearchExploreContext: SettingBase
    {

        public enum PageOP
        {
            NONE=0,
            CAT=1,
            ITEM=2
        };

        public enum PageExploreTypes
        {
            NOCAT=0,
            CAT=1
        }

        HashSet<String> m_filterCats = new HashSet<string>();

        public HashSet<String> FilterCats
        {
            get { return m_filterCats; }
            set { m_filterCats = value; }
        }


        private PageExploreTypes m_exploreType = PageExploreTypes.NOCAT;

        protected PageExploreTypes ExploreType
        {
            get { return m_exploreType; }
            set { m_exploreType = value; }
        }


        private Hashtable m_selectedCats = new Hashtable();

        protected Hashtable SelectedCats
        {
            get { return m_selectedCats; }
            set { m_selectedCats = value; }
        }
        private String contextName="";

        protected String ContextName
        {
            get { return contextName; }
            set { contextName = value; }
        }

        protected Stack<PageOP> pageOperations = new System.Collections.Generic.Stack<PageOP>();

        public PageOP PickOP()
        {
            if (pageOperations.Count == 0)
                return PageOP.NONE;
            return pageOperations.Peek();
        }
        public PageOP popPageOP()
        {
            if (pageOperations.Count == 0)
                return PageOP.NONE;
            return pageOperations.Pop();
        }
        public void pushPageOP(PageOP op)
        {
            pageOperations.Push(op);
        }
        //---------------------------------------------------------------
        protected Hashtable m_catPages = new Hashtable();
        protected  Stack<Hashtable> m_waitingPages = new System.Collections.Generic.Stack<Hashtable>();
        protected String pickURL(Hashtable ht)
        {
            String url = (String)ht["url"];
            if (url != null)
                return url;
            ICollection cc = ht.Values;
            foreach (Object v in cc)
            {
                if (v is String)
                {
                    String vv = (String)v;
                    if (vv.StartsWith("http"))
                    {
                        return vv;
                    }
                }
            }
            return null;
        }
        public String getCurrentPageURL()
        {
            if(m_waitingPages.Count==0)
                return null;
            Hashtable ht = m_waitingPages.Peek();
            String result = pickURL(ht);
            if(result==null)
                throw new Exception("Stack not empty but no valid url!");
            return result;
        }
        //--------------------------------------------------------------
        public Hashtable popURLConfig()
        {
            if (m_waitingPages.Count == 0)
                return null;
            return m_waitingPages.Pop();
        }
        public int  pushURLConfig(Hashtable ht)
        {
            m_waitingPages.Push(ht);
            return m_waitingPages.Count;
        }
        public int pushURL(String url)
        {
            Hashtable ht = new Hashtable();
            ht["url"] = url;
            return pushURLConfig(ht);
        }
        //--------------------------------------------------------------
        




    }
}
